package com.bee.shepherd;

import com.qunar.finance.nbdata.security.core.api.service.EncryptToNbClient;
import com.qunar.finance.nbdata.security.core.api.service.NbInfoDecryptClient;
import com.qunar.finance.nbdata.security.core.api.service.impl.EncryptToNbClientImpl;
import com.qunar.finance.nbdata.security.core.api.service.impl.NbInfoDecryptClientImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

import java.util.concurrent.ThreadPoolExecutor;

/**
 *
 */
@Configuration
public class ExtBeanConfiguration {

    @Value("${security.token}")
    private String securityToken;

    //加解密相关
//    @Bean
    public NbInfoDecryptClient nbInfoDecryptClient() throws IllegalAccessException {
        return new NbInfoDecryptClientImpl(securityToken);
    }
    //加解密相关
//    @Bean
    public EncryptToNbClient encryptToNbClient(){
        return new EncryptToNbClientImpl(securityToken);
    }

    @Bean
    public LocalValidatorFactoryBean validator()throws IllegalAccessException{
        return new LocalValidatorFactoryBean();
    }



    //为@Async注解服务的线程池
    @Bean(name = "quickPoolExecutor")
    public AsyncTaskExecutor quickPoolExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("quickPoolExecutor-");
        executor.setCorePoolSize(10);
        executor.setMaxPoolSize(10);
        executor.setKeepAliveSeconds(60);
        executor.setQueueCapacity(500);
        executor.setRejectedExecutionHandler( new ThreadPoolExecutor.DiscardPolicy());
        return executor;
    }
    //为@Async注解服务的线程池
    @Bean(name = "commonPoolExecutor")
    public AsyncTaskExecutor commonPoolExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setThreadNamePrefix("commonPoolExecutor-");
        executor.setCorePoolSize(5);
        executor.setMaxPoolSize(5);
        executor.setKeepAliveSeconds(60);
        executor.setQueueCapacity(500);
        executor.setRejectedExecutionHandler( new ThreadPoolExecutor.DiscardPolicy());
        return executor;
    }
}
